package com.lcredu.homework.service;

import com.lcredu.homework.dao.BuyerDao;
import com.lcredu.homework.dao.GoodsDao;
import com.lcredu.homework.dao.SellerDao;
import com.lcredu.homework.dao.TaoBaoDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author 黎长容
 * @date 2025-07-17 23:51
 */
@Service
public class GoodsService {
    @Resource
    private BuyerDao buyerDao;
    @Resource
    private GoodsDao goodsDao;
    @Resource
    private SellerDao sellerDao;
    @Resource
    private TaoBaoDao taoBaoDao;


    @Transactional
    public void buy(Integer buyerId, Integer goodsId, Integer sellerId, Integer num) {
        //0.查询商品价格
        Float price = goodsDao.queryPrice(goodsId);
        float money = price * num;
        System.out.println("应付金额=" + money);
        //1.顾客买东西,顾客扣钱
        buyerDao.updateMoney(buyerId, money);
        //2.商品库存减少
        goodsDao.updateAmount(goodsId, num);
        //3.淘宝收到钱
        double taoBaoMoney = money * 0.1;
        taoBaoDao.updatePrice(sellerId, taoBaoMoney);
        money -= taoBaoMoney;
        //4.商家收到钱
        sellerDao.updateMoney(buyerId, money);

    }
    
}
